package com.android.jianboshi.ui.activity;

import android.view.View;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.android.baselibrary.tool.CommToast;
import com.android.baselibrary.tool.SPUtil;
import com.android.baselibrary.util.Util;
import com.android.baselibrary.view.Comm_EditView;
import com.android.baselibrary.view.Comm_HeadView;
import com.android.baselibrary.view.MultiStateView;
import com.android.jianboshi.R;
import com.android.jianboshi.constants.SPConstants;

import butterknife.BindView;
import butterknife.OnClick;

public class SettingActivity extends BaseActivity {

    @BindView(R.id.multiplestatusView)
    MultiStateView multiplestatusView;
    @BindView(R.id.comm_title)
    Comm_HeadView comm_title;
    @BindView(R.id.et_weight)
    Comm_EditView et_weight;
    @BindView(R.id.radiogroup_repeat)
    RadioGroup radiogroup_repeat;
    @BindView(R.id.tv_unit)
    TextView tv_unit;


    int unit;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_setting;
    }

    @Override
    protected void initView() {
        comm_title.setTitle(getString(R.string.mine_menu_setting));

        unit = SPUtil.getIntValue(mContext, SPConstants.Unit, 0);
        radiogroup_repeat.check(radiogroup_repeat.getChildAt(unit).getId());

        radiogroup_repeat.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
                switch (checkedId) {
                    case R.id.tb_repeat1:
                        unit = 0;
                        tv_unit.setText(getString(R.string.setting_power_unit1));
                        try {
                            String str = et_weight.getText();
                            double weight = Double.parseDouble(str);
                            double weight1 = weight / 2.20462262185;
                            et_weight.setText(Util.numDecimalFormat(weight1+ "",1));
                        }
                        catch (Exception e)
                        {

                        }
                        break;
                    case R.id.tb_repeat2:
                        unit = 1;
                        tv_unit.setText(getString(R.string.setting_power_unit2));
                        try {
                            String str = et_weight.getText();
                            double weight = Double.parseDouble(str);
                            double weight1 = weight * 2.20462262185;
                            et_weight.setText(Util.numDecimalFormat(weight1+ "",1));
                        }
                        catch (Exception e)
                        {

                        }
                        break;
                }
            }
        });
    }

    @Override
    protected void initData() {
        getData();
    }

    private void getData() {
        String weight = SPUtil.getStringValue(mContext, SPConstants.User_Weight, "60.0");
        String weight1 = SPUtil.getStringValue(mContext, SPConstants.User_Weight1, "60.0");
        if (unit == 0) {
            et_weight.setText(weight);
        } else {
            et_weight.setText(weight1);
        }
    }

    @OnClick({R.id.csb_save})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.csb_save:
                //设置单位
                SPUtil.putValue(mContext, SPConstants.Unit, unit);
                //设置体重
                String str = et_weight.getText();
                try {
                    double weight = Double.parseDouble(str);
                    if (unit == 0) {
                        SPUtil.putValue(mContext, SPConstants.User_Weight, weight + "");
                    } else {
                        SPUtil.putValue(mContext, SPConstants.User_Weight1, weight + "");
                        double weight1 = weight / 2.20462262185;
                        SPUtil.putValue(mContext, SPConstants.User_Weight, weight1 + "");
                    }

                    CommToast.showToast(mContext, getString(R.string.setting_save_success));
                } catch (Exception e) {
                    CommToast.showToast(mContext, getString(R.string.setting_weight_tips));
                    return;
                }
                finish();
                break;
        }
    }
}
